package org.jeecg.modules.tencent;

import cn.hutool.json.JSONUtil;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.tencent.response.IMBaseResponse;
import org.jeecg.modules.abs.TencentCallBackAbstract;
import org.jeecg.modules.tencent.enums.IMCallbackEnum;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Map;

/**
 * @author guoyh
 * @ClassName TencentCallbackContext
 * @Description
 * @since 2020/8/29 00:09
 **/
@Component
@Slf4j
public class TencentCallbackContext {

    private Map<String,TencentCallBackAbstract> handleMap = Maps.newHashMap();

    @Resource
    private ApplicationContext applicationContext;

    @PostConstruct
    public void init(){
        Map<String,TencentCallBackAbstract> abstractMap = applicationContext.getBeansOfType(TencentCallBackAbstract.class);
        abstractMap.forEach((k,v)->{
            String key = v.getHandleClass().getKey();
            handleMap.put(key,v);
        });
        log.info("微信回告实现类registed ====>{}", JSONUtil.toJsonStr(handleMap));
    }

    public IMBaseResponse execute(String sdkAppId,String callBackCommand,String contentType, String clientIP,String optPlatform,String body){
        IMCallbackEnum imCallbackEnum = IMCallbackEnum.getInstance(callBackCommand);
        return handleMap.get(imCallbackEnum.getKey()).callBack(sdkAppId, callBackCommand, contentType, clientIP, optPlatform, body);
    }
}
